package com.hailiao.beidou.message.ui.page.chat

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.d10ng.compose.ui.AppColor
import com.d10ng.compose.ui.AppShape
import com.d10ng.compose.ui.AppText
import com.d10ng.compose.ui.PageTransitions
import com.d10ng.compose.ui.base.Button
import com.d10ng.compose.ui.base.ButtonSize
import com.d10ng.compose.ui.base.ButtonType
import com.d10ng.compose.ui.base.Cell
import com.d10ng.compose.ui.base.CellGroup
import com.d10ng.compose.ui.navigation.NavBar
import com.hailiao.beidou.message.R
import com.hailiao.beidou.message.ui.view.BDStatusBar
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph

/**
 * 发送位置报告页
 * @Author d10ng
 * @Date 2024/3/13 13:40
 */
@RootNavGraph
@Destination(style = PageTransitions::class)
@Composable
fun SendLocationScreen(
    model: SendLocationScreenViewModel = viewModel()
) {
    val lastSuccessTimeText by model.lastSuccessTimeTextFlow.collectAsState()

    SendLocationScreenView(
        lastSuccessTimeText = lastSuccessTimeText,
        onClickClose = { model.onClickClose() }
    )

    // 拦截返回事件
    BackHandler { }
}

@Composable
private fun SendLocationScreenView(
    lastSuccessTimeText: String = "--",
    onClickClose: () -> Unit = {}
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(AppColor.Neutral.bg)
            .navigationBarsPadding()
    ) {
        NavBar(title = "位置报告")
        BDStatusBar()
        CellGroup(border = false) {
            Cell(
                title = "上次发送成功时间",
                value = lastSuccessTimeText,
                border = false
            )
        }
        Column(
            modifier = Modifier
                .fillMaxSize()
                .weight(1f),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Image(
                painter = painterResource(id = R.drawable.send_location_img),
                contentDescription = "send location",
                modifier = Modifier
                    .padding(bottom = 12.dp)
                    .size(150.dp),
                contentScale = ContentScale.Fit
            )
            Text(
                text = "已开启位置报告",
                style = AppText.Bold.Primary.large,
                modifier = Modifier.padding(bottom = 25.dp)
            )
            Text(
                text = "每间隔北斗卡频度时间自动发送一次位置报告。\n如需使用APP的其他功能，请先关闭位置报告。",
                style = AppText.Normal.Tips.default,
                textAlign = TextAlign.Center,
                modifier = Modifier.padding(bottom = 30.dp)
            )
            Button(
                text = "关闭位置报告",
                type = ButtonType.DEFAULT,
                size = ButtonSize.BIG,
                plain = true,
                hairline = false,
                shape = AppShape.RC.Cycle,
                onClick = onClickClose,
                modifier = Modifier.padding(bottom = 30.dp).width(249.dp)
            )
        }
    }
}

@Preview
@Composable
private fun SendLocationScreenPreview() {
    SendLocationScreenView()
}